# -----------------------------------------------------------------------------------
# TODO...
# -----------------------------------------------------------------------------------

# ----------------------------------------------------------
# The argument 'DB_TYPE' defines which database to connect :
# - 'sqlite' : SQLite database
# - 'mongodb' : MongoDB database
# - 'postgresql' : PostgreSQL database
# - 'mysql' : MySQL database
# - 'mariadb' : MariaDB database
# - 'oracle' : Oracle database
# - 'mssqlserver' : Microsoft SQL Server database
ARG DB_TYPE

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Start from QxOrm Docker image with full C++ development environment
FROM qxorm/ubuntu-with-qxorm-dev-qxblogrestapi AS dev_base
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# ------------------------------------------------------------------
# The environment variable 'QX_DB' defines which database to connect
ENV QX_DB=${DB_TYPE}

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Some databases require to rebuild QxOrm library with specific compilation option
FROM dev_base AS dev_clean
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# --------------------------
# Clear previous cmake build
RUN rm -rfv $QX_DIR/test/build/debug/*
RUN rm -rfv $QX_DIR/test/build/release/*
RUN rm -rfv $QX_DIR/test/_bin/*
RUN rm -rfv $QX_DIR/lib/*

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# New Docker stage for SQLite database
FROM dev_base AS dev_sqlite
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# Nothing to do here

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# New Docker stage for MongoDB database
FROM dev_clean AS dev_mongodb
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# -----------------------------
# Install on the Docker image :
# - 'libmongoc' and 'libbson' : MongoDB C Driver and BSON library
# - 'pkg-config' : used by cmake when MongoDB is used
RUN apt update -y \
    && apt upgrade -y \
    && apt install -y \
      libmongoc-dev \
      pkg-config \
    && apt clean -y \
    && rm -rf /var/lib/apt/lists/*

# --------------------------------------
# Define environment variables for cmake
ENV QX_CMAKE_OPTIONS_RELEASE="${QX_CMAKE_OPTIONS_RELEASE} -D_QX_ENABLE_MONGODB=1 -D_QX_TEST_WITH_MONGODB=1"
ENV QX_CMAKE_OPTIONS_DEBUG="${QX_CMAKE_OPTIONS_DEBUG} -D_QX_ENABLE_MONGODB=1 -D_QX_TEST_WITH_MONGODB=1"
ENV CMAKE_OPTIONS="${QX_CMAKE_OPTIONS_RELEASE}"

# -----------------------------------------------------------------------------------
# Build QxOrm library and example project with 'QxHttpServer' and 'QxRestApi' modules
# Documentation about 'QxHttpServer' module : https://www.qxorm.com/qxorm_en/manual.html#manual_96
# Documentation about 'QxRestApi' module : https://www.qxorm.com/qxorm_en/manual.html#manual_97
USER $QX_USER
WORKDIR $QX_DIR/test/build/debug/
WORKDIR $QX_DIR/test/build/release/
RUN cmake ../.. $CMAKE_OPTIONS
RUN cmake --build . --config $CMAKE_CONFIG
USER root

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# New Docker stage for PostgreSQL database
FROM dev_base AS dev_postgresql
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# -----------------------------
# Install on the Docker image :
# - 'libqtXsqlX-psql' : Qt SQL driver for PostgreSQL database
RUN apt update -y \
    && apt upgrade -y \
    && apt install -y \
      libqt${QT_VERSION_MAJOR}sql${QT_VERSION_MAJOR}-psql \
    && apt clean -y \
    && rm -rf /var/lib/apt/lists/*

# ----------------------------------------------------------------
# Copy SQL database schema generated by QxEntityEditor application
WORKDIR $QX_APP_PATH/sql/
COPY --chown=$QX_USER:$QX_USER ./sql/qxBlog_postgresql_db_schema_full.sql .

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# New Docker stage for MySQL database
FROM dev_base AS dev_mysql
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# -----------------------------
# Install on the Docker image :
# - 'libqtXsqlX-mysql' : Qt SQL driver for MySQL and MariaDB database
RUN apt update -y \
    && apt upgrade -y \
    && apt install -y \
      libqt${QT_VERSION_MAJOR}sql${QT_VERSION_MAJOR}-mysql \
    && apt clean -y \
    && rm -rf /var/lib/apt/lists/*

# ----------------------------------------------------------------
# Copy SQL database schema generated by QxEntityEditor application
WORKDIR $QX_APP_PATH/sql/
COPY --chown=$QX_USER:$QX_USER ./sql/qxBlog_mysql_db_schema_full.sql .

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# New Docker stage for MariaDB database (same as MySQL)
FROM dev_mysql AS dev_mariadb
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# Nothing to do here

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Final Docker stage with selected database type
FROM dev_${DB_TYPE} AS dev_final
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# ------------------------------------------------------------------
# The environment variable 'QX_DB' defines which database to connect
ENV QX_DB=${DB_TYPE}

# ----------------------------------------
# This is where 'entrypoint.sh' is located
WORKDIR $QX_SCRIPTS/
